using System.Timers;

namespace MEA.AE_Data
{
	public class Engine12 : IEngine12
	{
		private CANMessage canMsgEng1;

		private CANMessage canMsgEng2;

		private double propSpeed;

		private string propSpeedText = "not available";

		private double oilPress;

		private string oilPressText = "not available";

		private double oilTemp;

		private string oilTempText = "not available";

		private double coolTemp;

		private string coolTempText = "not available";

		private double gearboxOilTemp;

		private string gearboxOilTempText = "not available";

		private string cautionLamp = "not available";

		private int cautioLampWert = 2;

		private string fuelPressWarn = "not available";

		private int fuelPressWarnWert = 2;

		private string ecuStatus = "not available";

		private ConfigBase.EEcuStatusWert ecuStatusWert = ConfigBase.EEcuStatusWert.eecuStatusnotAvailable;

		private string voterOutput = "not available";

		private int voterOutputWert = 2;

		private double batVolt;

		private string batVoltText = "not available";

		private double engPerf;

		private string engPerfText = "not available";

		private double fuelFlow;

		private string fuelFlowText = "not available";

		private double oilLevel;

		private string oilLevelText = "not available";

		private uint eng1_id;

		private uint eng2_id;

		public Timer timer;

		private byte[] canMsgEng1Old;

		private byte[] canMsgEng2Old;

		public CANMessage ENG1
		{
			get
			{
				return canMsgEng1;
			}
			set
			{
				canMsgEng1 = value;
			}
		}

		public CANMessage ENG2
		{
			get
			{
				return canMsgEng2;
			}
			set
			{
				canMsgEng2 = value;
			}
		}

		public double PropSpeed
		{
			get
			{
				return propSpeed;
			}
			set
			{
				propSpeed = value;
			}
		}

		public string PropSpeedText
		{
			get
			{
				return propSpeedText;
			}
			set
			{
				propSpeedText = value;
			}
		}

		public double OilPress
		{
			get
			{
				return oilPress;
			}
			set
			{
				oilPress = value;
			}
		}

		public string OilPressText
		{
			get
			{
				return oilPressText;
			}
			set
			{
				oilPressText = value;
			}
		}

		public double OilTemp
		{
			get
			{
				return oilTemp;
			}
			set
			{
				oilTemp = value;
			}
		}

		public string OilTempText
		{
			get
			{
				return oilTempText;
			}
			set
			{
				oilTempText = value;
			}
		}

		public double CoolTemp
		{
			get
			{
				return coolTemp;
			}
			set
			{
				coolTemp = value;
			}
		}

		public string CoolTempText
		{
			get
			{
				return coolTempText;
			}
			set
			{
				coolTempText = value;
			}
		}

		public double GearboxOilTemp
		{
			get
			{
				return gearboxOilTemp;
			}
			set
			{
				gearboxOilTemp = value;
			}
		}

		public string GearboxOilTempText
		{
			get
			{
				return gearboxOilTempText;
			}
			set
			{
				gearboxOilTempText = value;
			}
		}

		public string CautionLamp
		{
			get
			{
				return cautionLamp;
			}
			set
			{
				cautionLamp = value;
			}
		}

		public int CautioLampWert
		{
			get
			{
				return cautioLampWert;
			}
			set
			{
				cautioLampWert = value;
			}
		}

		public string FuelPressWarn
		{
			get
			{
				return fuelPressWarn;
			}
			set
			{
				fuelPressWarn = value;
			}
		}

		public int FuelPressWarnWert
		{
			get
			{
				return fuelPressWarnWert;
			}
			set
			{
				fuelPressWarnWert = value;
			}
		}

		public string EcuStatus
		{
			get
			{
				return ecuStatus;
			}
			set
			{
				ecuStatus = value;
			}
		}

		public ConfigBase.EEcuStatusWert EcuStatusWert
		{
			get
			{
				return ecuStatusWert;
			}
			set
			{
				ecuStatusWert = value;
				EngFunc2();
			}
		}

		public string VoterOutput
		{
			get
			{
				return voterOutput;
			}
			set
			{
				voterOutput = value;
			}
		}

		public int VoterOutputWert
		{
			get
			{
				return voterOutputWert;
			}
			set
			{
				voterOutputWert = value;
			}
		}

		public double BatVolt
		{
			get
			{
				return batVolt;
			}
			set
			{
				batVolt = value;
			}
		}

		public string BatVoltText
		{
			get
			{
				return batVoltText;
			}
			set
			{
				batVoltText = value;
			}
		}

		public double EngPerf
		{
			get
			{
				return engPerf;
			}
			set
			{
				engPerf = value;
			}
		}

		public string EngPerfText
		{
			get
			{
				return engPerfText;
			}
			set
			{
				engPerfText = value;
			}
		}

		public double FuelFlow
		{
			get
			{
				return fuelFlow;
			}
			set
			{
				fuelFlow = value;
			}
		}

		public string FuelFlowText
		{
			get
			{
				return fuelFlowText;
			}
			set
			{
				fuelFlowText = value;
			}
		}

		public double OilLevel
		{
			get
			{
				return oilLevel;
			}
			set
			{
				oilLevel = value;
			}
		}

		public string OilLevelText
		{
			get
			{
				return oilLevelText;
			}
			set
			{
				oilLevelText = value;
			}
		}

		public Engine12(CANMessage i_engMsg1, CANMessage i_engMsg2)
		{
			canMsgEng1 = i_engMsg1;
			canMsgEng2 = i_engMsg2;
			eng1_id = i_engMsg1.ID;
			eng2_id = i_engMsg2.ID;
			canMsgEng1.MessageReceive += OnMessageReceive;
			canMsgEng2.MessageReceive += OnMessageReceive;
			timer = new Timer(1000.0);
			timer.Elapsed += timer_Elapsed;
			timer.AutoReset = false;
			timer.Start();
		}

		private void timer_Elapsed(object sender, ElapsedEventArgs e)
		{
			ecuStatusWert = ConfigBase.EEcuStatusWert.eecuStatusnotAvailable;
			ecuStatus = "not available";
		}

		private void OnMessageReceive(object o)
		{
			if (o is CANMessage)
			{
				timer.Interval = 1000.0;
				timer.Start();
				CANMessage cANMessage = (CANMessage)o;
				if (cANMessage.ID == eng1_id)
				{
					EngFunc1();
				}
				else if (cANMessage.ID == eng2_id)
				{
					EngFunc2();
				}
			}
		}

		private void EngFunc1()
		{
			PropSpeed = canMsgEng1[0] + canMsgEng1[1] * 256;
			PropSpeedText = string.Format("{0} [rpm]", PropSpeed);
			if (PropSpeed == 65535.0)
			{
				PropSpeedText = "not available";
			}
			OilPress = (((canMsgEng1[3] & 0xF) << 8) + canMsgEng1[2]) * 5;
			OilPressText = string.Format("{0} [hPa]", OilPress);
			if (OilPress == 20475.0)
			{
				OilPressText = "not available";
			}
			OilTemp = canMsgEng1[4] - 50;
			OilTempText = string.Format("{0} [°C]", OilTemp);
			if (OilTemp == 205.0)
			{
				OilTempText = "not available";
			}
			CoolTemp = canMsgEng1[5] - 50;
			CoolTempText = string.Format("{0} [°C]", CoolTemp);
			if (CoolTemp == 205.0)
			{
				CoolTempText = "not available";
			}
			GearboxOilTemp = canMsgEng1[6] - 50;
			GearboxOilTempText = string.Format("{0} [°C]", GearboxOilTemp);
			if (GearboxOilTemp == 205.0)
			{
				GearboxOilTempText = "not available";
			}
		}

		private void EngFunc2()
		{
			BatVolt = (double)(int)canMsgEng2[0] * 0.2;
			BatVoltText = string.Format("{0:F1} [V]", BatVolt);
			if (BatVolt == 51.0)
			{
				BatVoltText = "not available";
			}
			switch (canMsgEng2[3] & 3)
			{
			case 0:
				CautionLamp = "off";
				CautioLampWert = 0;
				break;
			case 1:
				CautionLamp = "on";
				CautioLampWert = 1;
				break;
			default:
				CautionLamp = "not available";
				CautioLampWert = 2;
				break;
			}
			switch ((canMsgEng2[2] >> 4) & 3)
			{
			case 0:
				FuelPressWarn = "O.K.";
				FuelPressWarnWert = 0;
				break;
			case 1:
				FuelPressWarn = "too low";
				FuelPressWarnWert = 1;
				break;
			default:
				FuelPressWarn = "not available";
				FuelPressWarnWert = 2;
				break;
			}
			switch ((canMsgEng2[3] >> 2) & 3)
			{
			case 0:
				ecuStatus = "active";
				ecuStatusWert = ConfigBase.EEcuStatusWert.eecuStatusactive;
				break;
			case 1:
				ecuStatus = "passive";
				ecuStatusWert = ConfigBase.EEcuStatusWert.eecuStatuspassive;
				break;
			default:
				ecuStatus = "not available";
				ecuStatusWert = ConfigBase.EEcuStatusWert.eecuStatusnotAvailable;
				break;
			}
			switch ((canMsgEng2[3] >> 4) & 3)
			{
			case 0:
				VoterOutput = "ECU A";
				VoterOutputWert = 0;
				break;
			case 1:
				VoterOutput = "ECU B";
				VoterOutputWert = 1;
				break;
			default:
				VoterOutput = "not available";
				VoterOutputWert = 2;
				break;
			}
			EngPerf = (double)(int)canMsgEng2[4] * 0.5;
			EngPerfText = string.Format("{0} [%]", EngPerf);
			if (EngPerf == 127.5)
			{
				EngPerfText = "not available";
			}
			FuelFlow = (double)((canMsgEng2[6] & 0xF) * 256 + canMsgEng2[5]) * 0.1;
			FuelFlowText = FuelFlow + " [L/h]";
			if (FuelFlow == 409.5)
			{
				FuelFlowText = "not available";
			}
			OilLevel = (double)(int)canMsgEng2[7] * 1.0;
		}
	}
}
